package com.tozed.util;

import com.tozed.test.FTApplication;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;
import android.graphics.Camera;
import android.hardware.CameraInfo;
import android.media.AudioManager;
import android.media.AudioSystem;
import android.os.StatFs;
import android.os.SystemProperties;
import android.os.storage.StorageManager;
import android.os.storage.StorageVolume;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;

public class Util
{
	private static final String TAG = "Util";
	
    public static final String FEATURE_LOCATION_GPS = PackageManager.FEATURE_LOCATION_GPS;
    public static final String FEATURE_SENSOR_ACCELEROMETER = PackageManager.FEATURE_SENSOR_ACCELEROMETER;
    public static final String FEATURE_SENSOR_COMPASS = PackageManager.FEATURE_SENSOR_COMPASS;
    public static final String FEATURE_SENSOR_GYROSCOPE = PackageManager.FEATURE_SENSOR_GYROSCOPE;
    public static final String FEATURE_SENSOR_LIGHT = PackageManager.FEATURE_SENSOR_LIGHT;
    public static final String FEATURE_SENSOR_PROXIMITY = PackageManager.FEATURE_SENSOR_PROXIMITY;
    public static final String FEATURE_HEADSET = "android.hardware.headset";
    public static final String FEATURE_HEADPHONE = "android.hardware.headphone";
    public static final String FEATURE_FM = "android.hardware.fmradio";
    public static final String FEATURE_FRONT_CAMERA = "android.hardware.front.camera";
    public static final String FEATURE_BACK_CAMERA = "android.hardware.back.camera";
    public static final String FEATURE_FRONT_FLASH = "android.hardware.front.flash";
    public static final String FEATURE_BACK_FLASH = "android.hardware.back.flash";
    
    private static final String PROPERTY_FM = "ro.installed.fm";
    private static final String DEFAULT_PROPERTY_VERSION_VALUE = "111";
    
    /**
     * Utility.
     * Called in order to get if a feature is supported or not.
     *
     * @param context the Context, that this is running within
     * @param feature The feature string
     * @return boolean If a feature is supported.
     */
    public static boolean hasFeature(Context context, String feature) {
        PackageManager mPackageManager = context.getPackageManager();
        if (FEATURE_LOCATION_GPS.equals(feature)) {
            final boolean mHasGps = mPackageManager.hasSystemFeature(FEATURE_LOCATION_GPS);
            return mHasGps;
        } else if (FEATURE_SENSOR_ACCELEROMETER.equals(feature)) {
            final boolean mHasAcceleratorSensor =  mPackageManager.hasSystemFeature(FEATURE_SENSOR_ACCELEROMETER);
            return mHasAcceleratorSensor;
        } else if (FEATURE_SENSOR_COMPASS.equals(feature)) {
            final boolean mHasCompassSensor =  mPackageManager.hasSystemFeature(FEATURE_SENSOR_COMPASS);
            return mHasCompassSensor;
        } else if (FEATURE_SENSOR_GYROSCOPE.equals(feature)) {
            final boolean mHasGyroscope =  mPackageManager.hasSystemFeature(FEATURE_SENSOR_GYROSCOPE);
            return mHasGyroscope;
        } else if (FEATURE_SENSOR_LIGHT.equals(feature)) {
            final boolean mHasLightSensor =  mPackageManager.hasSystemFeature(FEATURE_SENSOR_LIGHT);
            return mHasLightSensor;
        } else if (FEATURE_SENSOR_PROXIMITY.equals(feature)) {
            final boolean mHasProximitySensor =  mPackageManager.hasSystemFeature(FEATURE_SENSOR_PROXIMITY);
            return mHasProximitySensor;
        } else if (FEATURE_HEADSET.equals(feature)) {
            final boolean hasHeadset = !(AudioSystem.getDeviceConnectionState(AudioManager.DEVICE_OUT_WIRED_HEADSET, "")
                            == AudioSystem.DEVICE_STATE_UNAVAILABLE);
            return hasHeadset;
        } else if (FEATURE_HEADPHONE.equals(feature)) {
            final boolean hasHeadphone = !(AudioSystem.getDeviceConnectionState(AudioManager.DEVICE_OUT_WIRED_HEADPHONE, "")
                            == AudioSystem.DEVICE_STATE_UNAVAILABLE);
            return hasHeadphone;
        } else if (FEATURE_FM.equals(feature)) {
            String value = SystemProperties.get(PROPERTY_FM, DEFAULT_PROPERTY_VERSION_VALUE);
            final boolean hasFm = ((value != null) && (value.equals("1")));
            return hasFm;
//        } else if (FEATURE_FRONT_CAMERA.equals(feature)) {
//            boolean hasFrontCamera = false;
//            int id = getFrontCameraId();
//            if (id >= 0) {
//                hasFrontCamera = true;
//            }
//            
//            return hasFrontCamera;
//        } else if (FEATURE_BACK_CAMERA.equals(feature)) {
//            boolean hasBackCamera = false;
//            int id = getBackCameraId();
//            if (id >= 0) {
//                hasBackCamera = true;
//            }
//            
//            return hasBackCamera;
//        } else if (FEATURE_FRONT_FLASH.equals(feature)) {
//            boolean hasFrontFlash = false;
//            hasFrontFlash = isCameraWithFlash(getFrontCameraId());
//            return hasFrontFlash;
//        } else if (FEATURE_BACK_FLASH.equals(feature)) {
//            boolean hasBackFlash = false;
//            int id = getBackCameraId();
//            hasBackFlash = isCameraWithFlash(getBackCameraId());
//            
//            return hasBackFlash;
        } else {
            Log.v("Util", "hasFeature \'" + feature + "\' unknown feature");
        }

        return false;
    }
	    
	public static String getResString(int resId)
	{
		Context ctx = FTApplication.getAppContext();
		return ctx.getResources().getString(resId);
	}
	
	public static void setTextInUiThread(final Activity activity, final TextView mTextView, final String strText){
		activity.runOnUiThread(new Runnable(){
			public void run(){
				mTextView.setText(strText);
			}
		});
	}
	
	public static void showToastTextInUiThread(final Activity activity, final String strText){
		activity.runOnUiThread(new Runnable(){
			public void run(){
				Toast.makeText(activity.getApplicationContext(), strText, Toast.LENGTH_SHORT).show();
			}
		});
	}
	
	public static String getInternalTFlashPath(Context context) {
        StorageManager mStorageManager = (StorageManager)context.getSystemService(Context.STORAGE_SERVICE);
        Log.v(TAG, "getInternalTFlashPath mStorageManager = " + mStorageManager);
        final StorageVolume[] storageVolumes = mStorageManager.getVolumeList();
        Log.v(TAG, "getInternalTFlashPath storageVolumes.length = " + storageVolumes.length);
        for (StorageVolume volume : storageVolumes) {
            boolean isRemovable = volume.isRemovable();
            Log.v(TAG, "getInternalTFlashPath isRemovable = " + isRemovable);
            if (!isRemovable) {
                String path = volume.getPath();
                Log.v(TAG, "getInternalTFlashPath path = " + path);
                return path;
            }
        }

        return null;
    }
	
	public static String getExternalTFlashPath(Context context) {
        StorageManager mStorageManager = (StorageManager)context.getSystemService(Context.STORAGE_SERVICE);
        Log.v(TAG, "getExternalTFlashPath mStorageManager = " + mStorageManager);
        final StorageVolume[] storageVolumes = mStorageManager.getVolumeList();
        Log.v(TAG, "getExternalTFlashPath storageVolumes.length = " + storageVolumes.length);
        for (StorageVolume volume : storageVolumes) {
            boolean isRemovable = volume.isRemovable();
            Log.v(TAG, "getExternalTFlashPath isRemovable = " + isRemovable);
            if (isRemovable) {
                String path = volume.getPath();
                Log.v(TAG, "getExternalTFlashPath path = " + path);
                return path;
            }
        }

        return null;
    }
	
	@SuppressLint("NewApi")
	public static long getInternalTFlashCapability(Context context) {
        String path = getInternalTFlashPath(context);
        Log.v(TAG, "getInternalTFlashCapability path = " + path);
        if (path != null) {
            StatFs stat = new StatFs(path);
            long total = stat.getTotalBytes();
            Log.v(TAG, "getInternalTFlashCapability total = " + total);
            return total;
        }
        
        return 0;
    }
	
	@SuppressLint("NewApi")
	public static long getExternalTFlashCapability(Context context) {
        String path = getExternalTFlashPath(context);
        Log.v(TAG, "getExternalTFlashCapability path = " + path);
        if (path != null) {
            StatFs stat = new StatFs(path);
            long total = stat.getTotalBytes();
            Log.v(TAG, "getExternalTFlashCapability total = " + total);
            return total;
        }
        
        return 0;
    }
}
